package com.cdwh.joylife.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.cdwh.joylife.R;
import com.cdwh.joylife.ui.base.BaseFragment;
import com.cdwh.joylife.utils.UiUtils;
import com.rd.PageIndicatorView;
import com.socks.library.KLog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager;
import qiu.niorgai.StatusBarCompat;

import static com.blankj.utilcode.util.BarUtils.getActionBarHeight;
import static com.blankj.utilcode.util.BarUtils.getStatusBarHeight;

/**
 * Created by huxing on 2018/2/8.
 */

public class HomeFragment extends BaseFragment {
    @BindView(R.id.btnCity)
    TextView btnCity;
    @BindView(R.id.btnSearch)
    LinearLayout btnSearch;
    @BindView(R.id.viewSearch)
    LinearLayout viewSearch;
    @BindView(R.id.viewPager)
    AutoScrollViewPager viewPager;
    @BindView(R.id.pageIndicatorView)
    PageIndicatorView pageIndicatorView;
    private int[] images = {R.mipmap.icon_md_2, R.mipmap.icon_dw, R.mipmap.icon_dwa};

    @Override
    public void onReceive(Object data) {

    }

    @Override
    public void onReceive(Object data, int type) {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(View layoutView) {

//        ?attr/actionBarSize

        RelativeLayout.LayoutParams params =
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                        getActionBarHeight(getActivity()));
        KLog.e("---------getStatusBarHeight" + getStatusBarHeight());
        params.topMargin = getStatusBarHeight();
        viewSearch.setLayoutParams(params);

        PagerAdapter pagerAdapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return images.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
//                super.destroyItem(container, position, object);
//                container.removeViewAt(position);
            }

            @Override
            public Object instantiateItem(ViewGroup container, final int position) {
                ImageView iv = new ImageView(getActivity());
                iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));
//                Glide.with(App.getContext()).load(mBanner.get(position).getPicUrl()).into(iv);
                iv.setImageResource(images[position]);
                container.addView(iv);
                return iv;
            }
        };
        viewPager.setAdapter(pagerAdapter);
        viewPager.startAutoScroll();
//        viewPager.setCycle(false);
        viewPager.setBorderAnimation(false);
        viewPager.setScrollDurationFactor(AutoScrollViewPager.SLIDE_BORDER_MODE_CYCLE);
        viewPager.setInterval(2000);
        pagerAdapter.notifyDataSetChanged();
        pageIndicatorView.setViewPager(viewPager);
    }

    @Override
    protected void loadData(int page) {

    }


    @OnClick({R.id.btnCity, R.id.btnSearch})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btnCity:
                break;
            case R.id.btnSearch:
                break;
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            StatusBarCompat.translucentStatusBar(getActivity());
        } else {
            StatusBarCompat.setStatusBarColor(getActivity(), UiUtils.getColor(R.color.colorPrimary));
        }
    }
}
